package com.oa.srms.apps.dao.staff.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.oa.srms.apps.dao.staff.IStaffInfoDao;
import com.oa.srms.apps.entity.staff.StaffInfoValue;
import com.oa.srms.apps.constants.IDEDStates;

public class StaffInfoDaoImpl extends HibernateDaoSupport implements
		IStaffInfoDao {
	
	@Autowired
	private List<StaffInfoValue> list;
	
	private int totalNumber;

	@SuppressWarnings("unchecked")
	@Override
	public List<StaffInfoValue> verifyStaffInfo(final StaffInfoValue staffInfo)
			throws Exception {
		String hqlStr = "FROM StaffInfoValue siv WHERE 1=1";
		if (staffInfo.getStaffNo() != null) {
			hqlStr += " AND siv.staffNo = ?";
		}
		if (staffInfo.getStaffPwd() != null) {
			hqlStr += " AND siv.staffPwd = ?";
		}
		hqlStr += " AND siv.states = ?";
		final String hql = hqlStr;
		list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				int index = -1;
				if (staffInfo.getStaffNo() != null) {
					query.setString(++index, staffInfo.getStaffNo());
				}
				if (staffInfo.getStaffPwd() != null) {
					query.setString(++index, staffInfo.getStaffPwd());
				}
				query.setInteger(++index, IDEDStates.ENABLE);
				list = query.list();
				return list;
			}
		});
		return list;
	}

	@Override
	public Integer add(StaffInfoValue staffInfoValue) {
		if(staffInfoValue != null){
			this.getHibernateTemplate().save(staffInfoValue);
		}
		return staffInfoValue.getStaffId();
	}

	@Override
	public void edit(StaffInfoValue staffInfoValue) {
		if(staffInfoValue != null){
			this.getHibernateTemplate().update(staffInfoValue);
		}
	}

	@Override
	public StaffInfoValue queryByStaffId(Integer staffId) {
		return (StaffInfoValue) this.getHibernateTemplate().get(StaffInfoValue.class, staffId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<StaffInfoValue> queryStaffInfo(final StaffInfoValue staffInfo, final Integer rows, final Integer pageNo) {
		String hqlStr = "SELECT siv FROM StaffInfoValue siv WHERE 1=1";
		if (staffInfo.getStaffName() != null) {
			hqlStr += " AND siv.staffName = ?";
		}
		if (staffInfo.getPostInfo() != null) {
			hqlStr += " AND siv.postInfo = ? ";
		}
		if (staffInfo.getStates() != null) {
			hqlStr += " AND siv.states = ? ";
		}
		hqlStr += " AND siv.states <> ?  ORDER BY siv.staffNo DESC";
		final String hql = hqlStr;
		
		list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				int index = -1;
				if (staffInfo.getStaffName() != null) {
					query.setString(++index, staffInfo.getStaffName());
				}
				if (staffInfo.getPostInfo() != null) {
					query.setEntity(++index, staffInfo.getPostInfo());
				}
				if (staffInfo.getStates()  != null) {
					query.setInteger(++index, staffInfo.getStates());
				}
				query.setInteger(++index, IDEDStates.DELETE);
				query.setFirstResult(rows * (pageNo - 1));
				query.setMaxResults(rows);
				list = query.list();
				return list;
			}
		});
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Integer getTotalPageNumber(final StaffInfoValue staffInfo, final Integer rows) {
		int totalPageNumber = 0;
		String hqlStr = "SELECT siv FROM StaffInfoValue siv WHERE 1=1";
		if (staffInfo.getStaffName() != null) {
			hqlStr += " AND siv.staffName = ?";
		}
		if (staffInfo.getPostInfo() != null) {
			hqlStr += " AND siv.postInfo = ? ";
		}
		if (staffInfo.getStates() != null) {
			hqlStr += " AND siv.states = ? ";
		}
		hqlStr += " AND siv.states <> ?  ORDER BY siv.staffNo DESC";
		final String hql = hqlStr;
		
		list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				int index = -1;
				if (staffInfo.getStaffName() != null) {
					query.setString(++index, staffInfo.getStaffName());
				}
				if (staffInfo.getPostInfo() != null) {
					query.setEntity(++index, staffInfo.getPostInfo());
				}
				if (staffInfo.getStates()  != null) {
					query.setInteger(++index, staffInfo.getStates());
				}
				query.setInteger(++index, IDEDStates.DELETE);
				list = query.list();
				return list;
			}
		});
		totalNumber = list.size();
		if (totalNumber <= rows) {
			totalPageNumber = 1;
		} else {
			if (totalNumber % rows == 0) {
				totalPageNumber = totalNumber / rows;
			} else {
				totalPageNumber = totalNumber / rows + 1;
			}
		}
		return totalPageNumber;
	}
	
	@Override
	public Integer getTotalNumber() {
		return totalNumber;
	}

}
